<template>
  <!-- 授权服务器详情抽屉 -->  
  <div class="client-ssoAuthServer-detail">
    <el-drawer v-model="isShowDialog" size="80%" direction="ltr">
      <template #header>
        <h4>授权服务器详情</h4>
      </template>
      <el-descriptions
              class="margin-top"
              :column="3"
              border
              style="margin: 8px;"
      >        
          <el-descriptions-item :span="1">            
              <template #label>
                <div class="cell-item">
                  主键id
                </div>
              </template>
              {{ formData.id }}            
          </el-descriptions-item>        
          <el-descriptions-item :span="1">              
                <template #label>
                  <div class="cell-item">
                    授权服务名称
                  </div>
                </template>
                {{ proxy.getOptionValue(formData.serverName, serverNameOptions,'value','label') }}            
          </el-descriptions-item>        
          <el-descriptions-item :span="1">            
              <template #label>
                <div class="cell-item">
                  应用ID
                </div>
              </template>
              {{ formData.clientId }}            
          </el-descriptions-item>        
          <el-descriptions-item :span="1">            
              <template #label>
                <div class="cell-item">
                  应用密钥
                </div>
              </template>
              {{ formData.clientSecret }}            
          </el-descriptions-item>        
          <el-descriptions-item :span="1">            
              <template #label>
                <div class="cell-item">
                  引导认证页地址
                </div>
              </template>
              {{ formData.authUrl }}            
          </el-descriptions-item>        
          <el-descriptions-item :span="1">            
              <template #label>
                <div class="cell-item">
                  回调地址
                </div>
              </template>
              {{ formData.callbackUrl }}            
          </el-descriptions-item>        
          <el-descriptions-item :span="1">            
              <template #label>
                <div class="cell-item">
                  code换取accessToken地址
                </div>
              </template>
              {{ formData.codeTokenUrl }}            
          </el-descriptions-item>        
          <el-descriptions-item :span="1">              
                <template #label>
                  <div class="cell-item">
                    code换取请求方式
                  </div>
                </template>
                {{ proxy.getOptionValue(formData.codeTokenType, codeTokenTypeOptions,'value','label') }}            
          </el-descriptions-item>        
          <el-descriptions-item :span="1">            
              <template #label>
                <div class="cell-item">
                  刷新token地址
                </div>
              </template>
              {{ formData.refreshTokenUrl }}            
          </el-descriptions-item>        
          <el-descriptions-item :span="1">              
                <template #label>
                  <div class="cell-item">
                    刷新token请求方式
                  </div>
                </template>
                {{ proxy.getOptionValue(formData.refreshTokenType, refreshTokenTypeOptions,'value','label') }}            
          </el-descriptions-item>        
          <el-descriptions-item :span="1">            
              <template #label>
                <div class="cell-item">
                  创建人
                </div>
              </template>
              {{ formData.createdBy }}            
          </el-descriptions-item>        
          <el-descriptions-item :span="1">
            <template #label>
              <div class="cell-item">
                创建日期
              </div>
            </template>
            {{ proxy.parseTime(formData.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}
          </el-descriptions-item>        
          <el-descriptions-item :span="1">
            <template #label>
              <div class="cell-item">
                应用logo
              </div>
            </template>
            <el-image
                    style="width: 150px; height: 150px"
                    v-if="!proxy.isEmpty(formData.logo)"
                    :src="proxy.getUpFileUrl(formData.logo)"
                    fit="contain"></el-image>
          </el-descriptions-item>        
      </el-descriptions>
    </el-drawer>
  </div>
</template>
<script setup lang="ts">
  import { reactive, toRefs, defineComponent,ref,unref,getCurrentInstance,computed } from 'vue';
  import {ElMessageBox, ElMessage, FormInstance,UploadProps} from 'element-plus';  
  import {
    listSsoAuthServer,
    getSsoAuthServer,
    delSsoAuthServer,
    addSsoAuthServer,
    updateSsoAuthServer,    
  } from "/@/api/system/sso/client/ssoAuthServer";  
  import {getToken} from "/@/utils/gfast"  
  import {
    SsoAuthServerTableColumns,
    SsoAuthServerInfoData,
    SsoAuthServerTableDataState,
    SsoAuthServerEditState
  } from "/@/views/system/sso/client/ssoAuthServer/list/component/model"
  defineOptions({ name: "ApiV1SsoClientSsoAuthServerDetail"})  
  const props = defineProps({    
    serverNameOptions:{
      type:Array,
      default:()=>[]
    },    
    codeTokenTypeOptions:{
      type:Array,
      default:()=>[]
    },    
    refreshTokenTypeOptions:{
      type:Array,
      default:()=>[]
    },    
  })  
  const baseURL:string|undefined|boolean = import.meta.env.VITE_API_URL  
  const {proxy} = <any>getCurrentInstance()
  const formRef = ref<HTMLElement | null>(null);
  const menuRef = ref();  
  //图片上传地址
  const imageUrlLogo = ref('')
  //上传加载
  const upLoadingLogo = ref(false)  
  const state = reactive<SsoAuthServerEditState>({
    loading:false,
    isShowDialog: false,
    formData: {      
      id: undefined,      
      serverName: undefined,      
      clientId: undefined,      
      clientSecret: undefined,      
      authUrl: undefined,      
      callbackUrl: undefined,      
      codeTokenUrl: undefined,      
      codeTokenType: undefined,      
      refreshTokenUrl: undefined,      
      refreshTokenType: undefined,      
      createdBy: undefined,      
      createdAt: undefined,      
      updatedAt: undefined,      
      logo: undefined,      
      deletedAt: undefined,      
    },
    // 表单校验
    rules: {      
      serverName : [
          { required: true, message: "授权服务名称不能为空", trigger: "blur" }
      ],      
    }
  });
  const { isShowDialog,formData } = toRefs(state);
  // 打开弹窗
  const openDialog = (row?: SsoAuthServerInfoData) => {
    resetForm();
    if(row) {
      getSsoAuthServer(row.id!).then((res:any)=>{
        const data = res.data;        
        data.createdBy = data.createdUser?.userNickname        
        //单图地址赋值
        imageUrlLogo.value = data.logo ? proxy.getUpFileUrl(data.logo) : ''        
        state.formData = data;
      })
    }
    state.isShowDialog = true;
  };
  // 关闭弹窗
  const closeDialog = () => {
    state.isShowDialog = false;
  };
  defineExpose({
    openDialog,
  });
  // 取消
  const onCancel = () => {
    closeDialog();
  };
  const resetForm = ()=>{
    state.formData = {      
      id: undefined,      
      serverName: undefined,      
      clientId: undefined,      
      clientSecret: undefined,      
      authUrl: undefined,      
      callbackUrl: undefined,      
      codeTokenUrl: undefined,      
      codeTokenType: undefined,      
      refreshTokenUrl: undefined,      
      refreshTokenType: undefined,      
      createdBy: undefined,      
      createdAt: undefined,      
      updatedAt: undefined,      
      logo: undefined,      
      deletedAt: undefined,      
    }
  };  
  //单图上传应用logo
  const handleAvatarSuccessLogo:UploadProps['onSuccess'] = (res, file) => {
    if (res.code === 0) {
      imageUrlLogo.value = URL.createObjectURL(file.raw!)
      state.formData.logo = res.data.path
    } else {
      ElMessage.error(res.msg)
    }
    upLoadingLogo.value = false
  }
  const beforeAvatarUploadLogo = () => {
    upLoadingLogo.value = true
    return true
  }  
  const setUpData = () => {
    return { token: getToken() }
  }  
</script>
<style scoped>  
  .client-ssoAuthServer-detail :deep(.avatar-uploader .avatar) {
    width: 178px;
    height: 178px;
    display: block;
  }
  .client-ssoAuthServer-detail :deep(.avatar-uploader .el-upload) {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }
  .client-ssoAuthServer-detail :deep(.avatar-uploader .el-upload:hover) {
    border-color: var(--el-color-primary);
  }
  .client-ssoAuthServer-detail :deep(.el-icon.avatar-uploader-icon) {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
  }  
  .client-ssoAuthServer-detail :deep(.el-form-item--large .el-form-item__label){
    font-weight: bolder;
  }
  .pic-block{
    margin-right: 8px;
  }
  .file-block{
    width: 100%;
    border: 1px solid var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
    margin-bottom: 5px;
    padding: 3px 6px;
  }
  .ml-2{margin-right: 5px;}
</style>